package com.bolun998300.personal_blog_springboot.controller.background;

import com.bolun998300.personal_blog_springboot.pojo.Blog;
import com.bolun998300.personal_blog_springboot.pojo.BlogType;
import com.bolun998300.personal_blog_springboot.service.BlogTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@RestController
public class BlogTypeController {

    @Autowired
    private BlogTypeService blogTypeService;

    @GetMapping("/blogTypes")
    public HashMap<String,Object> listByPage(int page,int limit){
        Page<BlogType> pageFromJPA = blogTypeService.listByPage(page,limit);
        HashMap<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",pageFromJPA.getTotalElements());
        map.put("data",pageFromJPA.getContent());
        return map;
    }

    @GetMapping("/blogTypesAll")
    public List<BlogType> getAll(){
        return blogTypeService.getAll();
    }


    @PostMapping("/blogTypes")
    public Object add(@RequestBody BlogType blogType){
        blogType.setCreateTime(new Date());
        blogTypeService.add(blogType);
        return blogType;
    }

    @GetMapping("/blogTypes/{id}")
    public BlogType get(@PathVariable int id){
        return blogTypeService.get(id);
    }




    @PutMapping("/blogTypes")
    public Object update(@RequestBody BlogType blogType){
        blogType.setUpdateTime(new Date());
        blogTypeService.update(blogType);
        return blogType;
    }

    @DeleteMapping("/blogTypes/{id}")
    public String delete(@PathVariable int id){
        blogTypeService.delete(id);
        return null;
    }

}
